package com.sxj.corejava.code08_常用类.String;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.Arrays;

/**
 * @author 石小俊
 * @date 2024年04月03日 13:52
 */
public class Test02_常用方法 {

    public static void main(String[] args) {
        String s = " Hello World and admin and jack and tom and lily ";
        System.out.println("字符串长度:" + s.length());
        // 去除字符串首尾的空格
        s = s.trim();
        System.out.println("字符串长度:" + s.length());

        System.out.println("and第一次出现的索引:" + s.indexOf("and"));
        System.out.println("and在索引15之后第一次出现的索引:" + s.indexOf("and", 15));
        System.out.println("and最后一次出现的索引:" + s.lastIndexOf("and"));
        System.out.println("截止到索引为35的位置时and最后一次出现的索引:" + s.lastIndexOf("and", 35));

        // 截取字符串
        System.out.println(s.substring(20));
        System.out.println(s.substring(20, 32));

        s = s.concat(" and alice");
        System.out.println(s);

        System.out.println("字符串s是否为空:" + s.isEmpty());
        String s1 = null;
        String s2 = "";
        // System.out.println("字符串是否为空:"+s1.isEmpty());    // NullPointerException
        System.out.println("字符串s2是否为空:" + s2.isEmpty());

        System.out.println("获取索引为10的字符:" + s.charAt(10));

        System.out.println("是否以H开头:" + s.startsWith("H"));    // 区分大小写
        System.out.println("是否以y结尾:" + s.endsWith("y"));
        System.out.println("是否以e结尾:" + s.endsWith("e"));
        System.out.println("字符串是否包含and:" + s.contains("and"));

        s1 = "admin";
        s2 = new String("admin");
        System.out.println(s1.equals(s2));

        String s3 = "Hello";
        String s4 = "hello";
        System.out.println(s3.equals(s4));
        System.out.println(s3.equalsIgnoreCase(s4));

        System.out.println("大写:" + s.toUpperCase());
        System.out.println("小写:" + s.toLowerCase());

        System.out.println(s.replace(" ", "-"));

        // 通过指定的字符串对原来的字符串进行分割,将其分割为数组
        String[] arr = s.split(" ");
        System.out.println(Arrays.toString(arr));
    }

}
